/**
 * 
 */
package fhq.hcmute.edu.vn.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import fhq.hcmute.edu.vn.activities.R;
import fhq.hcmute.edu.vn.system.MySystem;

/**
 * @author Hien Nguyen Nov 2, 2012
 */
public class FileAdapter extends ArrayAdapter<MySystem> {

	private Activity activity;
	private List<MySystem> objects;
	private ArrayList<String> strings;

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public FileAdapter(Activity activity, List<MySystem> objects) {
		super(activity, R.layout.gridview_item, objects);
		this.activity = activity;
		this.objects = objects;
	}

	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public List<MySystem> getObjects() {
		return objects;
	}

	public void setObjects(List<MySystem> objects) {
		this.objects = objects;
	}

	static class viewHolder {
		private TextView textView;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			row = inflater.inflate(R.layout.gridview_item, null);
			viewHolder holder = new viewHolder();
			holder.textView =
					(TextView) row.findViewById(R.id.txtGridViewItem);
			row.setTag(holder);
		}
		viewHolder holder = (viewHolder) row.getTag();
		MySystem mySystem = objects.get(position);
		holder.textView.setCompoundDrawablesWithIntrinsicBounds(0,
				mySystem.getIconUrl(), 0, 0);
		holder.textView.setText(mySystem.getName());
		return row;
	}

	public ArrayList<String> getStrings() {
		strings = new ArrayList<String>();
		for (int i = 0; i < objects.size(); i++) {
			strings.add(objects.get(i).getName());
		}
		return strings;
	}

	public MySystem getObjectByName(String name) {
		for (int i = 0; i < objects.size(); i++) {
			MySystem obj = objects.get(i);
			if (obj.getName().equals(name)) {
				return obj;
			}
		}
		return null;
	}
}
